<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/23 0023
 * Time: 下午 6:08
 */
namespace app\common\service;


use app\admin\model\Groupdetail;
use app\admin\model\Member;
use app\common\BaseHelper;
use app\common\Common;
use app\common\model\Order;
use app\common\model\Orderproductdetail;
use app\common\model\User;
use think\Controller;

class TemplateMessage extends Controller {
    //获取小程序模板库标题列表


    public static function GroupSpeedOfProgress($orderID,$chentuannum,$goups,$endDate){
        $access_token = Utils::getAccessToken();
        if (empty($access_token)) {
            return false;
        }
        $Groupdetail = Groupdetail::where('order_id',$orderID)->find();
        $join = [
            ['__PRODUCT__ b','b.id=a.product_id']
        ];
        $field = 'a.group_price,b.name,b.price';
        $productinfo = db('groupproduct')->alias('a')->join($join)->where('a.id',$Groupdetail['group_id'])->field($field)->find();
        $order = \app\admin\model\Order::get($orderID);
        if(!empty($order->prepay_id)){
            $formId = $order->prepay_id;
        }else{
            $formId = $order->form_id;
        }
        $member = Member::get($order->member_id);
        $shengyutime = $endDate - time();
        $shengyutime = Common::time2second($shengyutime);
        $shengyuperson = $chentuannum - $goups;
        $group_price = sprintf('%.2f',$productinfo['group_price']/100);
        $price = sprintf('%.2f',$productinfo['price']/100);
        $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $access_token;
        $postData['touser'] = $member->open_id;
        $postData['template_id'] = '24VjxQfmeMDqhuj2HpRcgq_2iYaR3oRq1Iz1ogkq0A8';
        $postData['form_id'] = $formId;
        $postData['data'] = [
            'keyword1' => ['value'=>$productinfo['name']],
            'keyword2' => ['value'=>$group_price . '元'],
            'keyword3' => ['value'=>$price . '元'],
            'keyword4' => ['value'=>$chentuannum . '人'],
            'keyword5' => ['value'=>'已有' . $goups . '人参加，还差' . $shengyuperson . '人成团'],
            'keyword6' => ['value'=>$shengyutime],
            'keyword7' => ['value'=>$order->pay_date]
        ];
        BaseHelper::curlPost($url,json_encode($postData));
    }


    public static function GroupSuccess($orderID,$chentuannum){
        $access_token = Utils::getAccessToken();
        if (empty($access_token)) {
            return false;
        }
        $Groupdetail = Groupdetail::where('order_id',$orderID)->find();
        $join = [
            ['__PRODUCT__ b','b.id=a.product_id']
        ];
        $field = 'b.name';
        $productinfo = db('groupproduct')->alias('a')->join($join)->where('a.id',$Groupdetail['group_id'])->field($field)->find();
        $order = \app\admin\model\Order::get($orderID);
        if(!empty($order->prepay_id)){
            $formId = $order->prepay_id;
        }else{
            $formId = $order->form_id;
        }
        $member = Member::get($order->member_id);
        $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $access_token;
        $postData['touser'] = $member->open_id;
        $postData['template_id'] = 'jf7YspIDdVXtakbvgUIaSg2tqGMfO25a_hBA3LfV__c';
        $postData['form_id'] = $formId;
        $postData['data'] = [
            'keyword1' => ['value'=>$productinfo['name']],
            'keyword2' => ['value'=>$chentuannum . '人'],
            'keyword3' => ['value'=>'已满'.$chentuannum . '人'],
            'keyword4' => ['value'=>'恭喜您已成功拼团'],
            'keyword5' => ['value'=>$order->pay_date]
        ];
        BaseHelper::curlPost($url,json_encode($postData));
    }


    /**
     * 拼团失败通知
     * @param $orderID
     * @param $group_hours
     * @param $group_nums
     * @return bool
     */
    public static function refundsendmessage($orderID,$group_hours,$group_nums){
        $order = Order::get($orderID);
        $access_token = Utils::getAccessToken();
        if (empty($access_token)) {
            return false;
        }

        $formId = $order->formId;
        $orderOrderproductdetail = Orderproductdetail::where('orderId',$order->id)->find();
        $user = User::get($order->createUser);
        $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $access_token;
        $total = ($order->total)/100;
        $postData['touser'] = $user->openId;
        $postData['template_id'] = 'iOWITscRQdwp_RdAYPof0IYeo9EG9_ZEkD6WVXakLEw';
        $postData['form_id'] = $formId;
        $postData['data'] = [
            'keyword1' => ['value'=>$orderOrderproductdetail->productName],
            'keyword2' => ['value'=>$total.'元'],
            'keyword3' => ['value'=>$group_hours.'小时内还没凑满'.$group_nums.'人参团'],
            'keyword4' => ['value'=>$order->create_time],
            'keyword5' => ['value'=>'点击查看详情']
        ];

        json_decode(BaseHelper::curlPost($url,json_encode($postData)),true);


    }

}